package com.tongshu.multiplefiles.utils;

import com.tongshu.multiplefiles.config.UploadConfig;
import org.springframework.context.annotation.Bean;

import java.io.*;
import java.util.UUID;

/**
 * 文件上传工具类
 */
public class FileUtils {
    public static void writeFile(String path, InputStream in) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        OutputStream os = new FileOutputStream(file);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        os.flush();
        os.close();
    }

    /**
     * 分块写入文件
     * @param target
     * @param targetSize
     * @param src
     * @param srcSize
     * @param chunks
     * @param chunk
     * @throws IOException
     */
    public static void writeWithBlock(String target, Long targetSize, InputStream src, Long srcSize, Integer chunks, Integer chunk) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(target,"rw");
        randomAccessFile.setLength(targetSize);
        if (chunk == chunks - 1) {
            randomAccessFile.seek(targetSize - srcSize);
        } else {
            randomAccessFile.seek(chunk * srcSize);
        }
        byte[] buf = new byte[1024];
        int len;
        while (-1 != (len = src.read(buf))) {
            randomAccessFile.write(buf,0,len);
        }
        randomAccessFile.close();
    }

    public static boolean writeWithBlock1(String target, Long targetSize, String source, Integer chunks, Integer chunk) throws IOException{
        checkExists(target);
        RandomAccessFile randomAccessFile = new RandomAccessFile(target, "rw");
        randomAccessFile.setLength(targetSize);
        File file = new File(UploadConfig.tempPath + source);

        if (!file.exists())
            return false;
        else {
            InputStream src = new FileInputStream(file);
            if (chunk == chunks) {
                randomAccessFile.seek(targetSize - file.length());
            } else {
                randomAccessFile.seek(chunk * file.length());
            }
            byte[] buff = new byte[1024];
            int len;
            while (-1 != (len = src.read(buff))) {
                randomAccessFile.write(buff, 0, len);
            }
            randomAccessFile.close();
        }
        return true;
    }

    /**
     * 生成随机文件名
     * @return
     */
    public static String generateFileName() {
        return UUID.randomUUID().toString();
    }

    /**
     * 比较前端的分块md5和本地文件的分块md5是否一致
     * @param remoteMd5
     * @param chunkFile
     * @return
     */
    public static boolean checkMd5(String remoteMd5, File chunkFile) {
        return FileMd5Util.getFileMD5(chunkFile).equals(remoteMd5);
    }

    private static void checkExists(String path) throws IOException{
        File file = new File(path);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
    }
}
